/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mini_game_fx;

import javafx.event.EventHandler;
import javafx.scene.input.MouseEvent;

/**
 * This is a JavaFX version of Mini game Framework KeyEventRelayer
 * class.
 *
 * The original MiniGameEventRelayerFx class implements MouseListener,
 * MouseDragListener, and KeyListener. Instead, JavaFX do override the
 * handle() method to control each of events.
 * 
 * @author Heecheon Kim
 * @author Taewan Kim
 * @author Jaewoong Shin
 */
public class MiniGameMouseEventRelayerFx implements EventHandler<MouseEvent>{
    
    // THE GAME FROM WHICH THE EVENTS WERE GENERATED
    private MiniGameFx game;
    
    /**
     * The constructor just sets up access to the game for which to respond
     * to.
     * 
     * @param initGameFx the game being played.
     */
    public MiniGameMouseEventRelayerFx(MiniGameFx initGameFx) {
        game = initGameFx;
    }
    
    /**
     * This method handles all types of mouse events by calling different
     * event methods according to the event type.
     * 
     * @param me Mouse event.
     */
    @Override
    public void handle(MouseEvent me) {
        if(me.getEventType() == MouseEvent.MOUSE_PRESSED){
//            System.out.println("Mouse pressed: x(" + (int)me.getX() + "), y(" + (int)me.getY() + ")");
            mousePressed(me);
        }
        else if(me.getEventType() == MouseEvent.MOUSE_ENTERED) {
            mouseEntered(me);
        }
        else if(me.getEventType() == MouseEvent.MOUSE_EXITED) {
            mouseExited(me);
        }
        else if(me.getEventType() == MouseEvent.MOUSE_RELEASED) {
            mouseReleased(me);
        }
        else if(me.getEventType() == MouseEvent.MOUSE_CLICKED) {
            mouseClicked(me);
        }
        else if(me.getEventType() == MouseEvent.MOUSE_MOVED) {
            mouseMoved(me);
        }
        else if(me.getEventType() == MouseEvent.MOUSE_DRAGGED) {
            mouseDragged(me);
        }
    }
    
    /**
     * This method will test to see if the mouse press was on one of the game
     * developer's registered buttons, and if it is, the appropriate event
     * handler will be executed. If not, the game program's custom response will
     * be executed in the appropriate data model class. Note that this response
     * makes sure to get a lock on the data before use.
     *
     * @param me Mouse event associated with mouse pressed.
     */
    public void mousePressed(MouseEvent me) {
            // LOCK THE DATA
        
    }
    
    /**
     * WE WILL NOT USE THIS METOHD. IF YOU WANT TO USE THE METHOD, OVERRIDE
     * THIS METHOD.
     * 
     * @param me Mouse event associated with mouse entered.
     */
    public void mouseEntered(MouseEvent me) {
        // DO NOTHING
    }
    
    /**
     * WE WILL NOT USE THIS METHOD. IF YOU WANT TO USE THE METHOD, OVERRIDE
     * THIS METHOD.
     * 
     * @param me Mouse event associated with mouse exited.
     */
    public void mouseExited(MouseEvent me) {
        // DO NOTHING
    }
    
    /**
     * WE WILL NOT USE THIS METHOD. IF YOU WANT TO USE THE METHOD, OVERRIDE
     * THIS METHOD.
     * 
     * @param me Mouse event associated with mouse released.
     */
    public void mouseReleased(MouseEvent me) {
        // DO NOTHING
    }
    
    /**
     * WE WILL NOT USE THIS METHOD. IF YOU WANT TO USE THE METHOD, OVERRIDE
     * THIS METHOD.
     * 
     * @param me Mouse event associated with mouse clicked.
     */
    public void mouseClicked(MouseEvent me) {
        // DO NOTHING
            game.beginUsingData();

            // GET THE COORDINATES
            int x = (int) me.getX();
            int y = (int) me.getY();
			
            // FIRST CHECK THE GUI BUTTONS
            boolean buttonClicked = game.processButtonPress(x, y);

            // IF IT WAS NOT A GUI BUTTON, THEN WE SHOULD
            // EXECUTE THE CUSTOM GAME RESPONSE
            //System.out.println("button Clicked: " + buttonClicked);
            if (!buttonClicked && game.getDataModel().inProgress()) {
                MiniGameDataModelFx data = game.getDataModel();
                data.checkMousePressOnSprites(game, x, y);
            }
            
//            me.consume();
            
            game.endUsingData();
    }
    
    /**
     * This guy demonstrates how to get pixel information about a
     * particular location of what's being drawing inside a panel.
     * 
     * @param me Location of mouse click in canvas.
     */
    public void mouseMoved(MouseEvent me) {
        game.getDataModel().setLastMouseX((int)me.getX());
        game.getDataModel().setLastMouseY((int)me.getY());
        game.getDataModel().updateDebugText(game);
    }
    
    /**
     * WE'RE NOT USING THIS GUY AT THE MOMENT, THOUGH YOU AMY CHOOSE TO.
     * IF YOU WANT TO USE THE METHOD, OVERRIDE THIS METHOD.
     * 
     * @param me Mouse event associated with mouse dragged.
     */
    public void mouseDragged(MouseEvent me) {
        // DO NOTHING
    }
}
